package com.web.admin.web.base.wechat;

import com.blue.core.dict.HttpMethod;
import com.blue.core.dict.annotation.DictParser;
import com.blue.http.annotation.Http;
import com.blue.http.message.HttpRequest;
import com.blue.http.message.View;
import com.blue.validation.ValidationUtil;
import com.blue.validation.group.SaveModel;
import com.blue.validation.group.UpdateModel;
import com.web.base.wechat.facade.WechatAutoMessageFacade;
import com.web.base.wechat.facade.WechatFacade;
import com.web.base.wechat.message.WechatAutoMessageRequest;
import com.web.base.wechat.message.WechatAutoMessageResponse;
import com.web.base.wechat.message.WechatResponse;
import com.web.core.util.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.List;

@Controller
@Http(url = "/base/wechat")
public class WechatAutoMessageController
{
	@Autowired
	private WechatAutoMessageFacade wechatAutoMessageFacade;
	@Autowired
	private WechatFacade wechatFacade;
	
	private DictParser dictParser = DictParser.getInstance();
	
	public WechatAutoMessageController()
	{
	}
	
	@Http(url = "/wechat_auto_message_list", method = HttpMethod.GET)
	public View listGet(HttpRequest request)
	{
		WechatAutoMessageRequest wechatAutoMessageRequest = request.getQueryObject(WechatAutoMessageRequest.class);
		List<WechatResponse> wechatList = wechatFacade.listAll();
		if (wechatAutoMessageRequest.getWechatId() == null && !wechatList.isEmpty())
		{
			wechatAutoMessageRequest.setWechatId(wechatList.get(0).getId());
		}
		return View.createBuilder().setView("/base/wechat/wechat_auto_message_list")
				.put("wechatList", wechatList)
				.put("map", dictParser.getStringMap())
				.put("model", wechatAutoMessageRequest)
				.put("page", wechatAutoMessageFacade.listPage(wechatAutoMessageRequest, Const.PAGE_START, Const.PAGE_SIZE))
				.build();
	}
	
	@Http(url = "/wechat_auto_message_list", method = HttpMethod.POST)
	public View listPost(HttpRequest request)
	{
		WechatAutoMessageRequest wechatAutoMessageRequest = request.getPostObject(WechatAutoMessageRequest.class);
		int start = request.getPostInt("start", Const.PAGE_START);
		return View.createBuilder().setView("/base/wechat/wechat_auto_message_list_grid")
				.put("page", wechatAutoMessageFacade.listPage(wechatAutoMessageRequest, start, Const.PAGE_SIZE))
				.build();
	}
	
	@Http(url = "/wechat_auto_message_get", method = HttpMethod.GET)
	public View get(HttpRequest request)
	{
		WechatAutoMessageRequest wechatAutoMessageRequest = request.getQueryObject(WechatAutoMessageRequest.class);
		List<WechatResponse> wechatList = wechatFacade.listAll();
		WechatAutoMessageResponse response = null;
		if (wechatAutoMessageRequest.getId() != null)
		{
			response = wechatAutoMessageFacade.get(wechatAutoMessageRequest.getId());
		}
		if (response == null)
		{
			response = new WechatAutoMessageResponse();
			response.setWechatId(wechatAutoMessageRequest.getWechatId());
		}
		return View.createBuilder().setView("/base/wechat/wechat_auto_message_get")
				.put("wechatList", wechatList)
				.put("model", response)
				.put("map", dictParser.getStringMap())
				.build();
	}
	
	@Http(url = "/wechat_auto_message_save", method = HttpMethod.POST)
	public void save(HttpRequest request)
	{
		WechatAutoMessageRequest wechatAutoMessageRequest = request.getPostObject(WechatAutoMessageRequest.class);
		WechatAutoMessageResponse response = null;
		if (wechatAutoMessageRequest.getId() == null)
		{
			ValidationUtil.valid(wechatAutoMessageRequest, SaveModel.class);
			response = wechatAutoMessageFacade.add(wechatAutoMessageRequest);
		}
		else
		{
			ValidationUtil.valid(wechatAutoMessageRequest, UpdateModel.class);
			response = wechatAutoMessageFacade.update(wechatAutoMessageRequest);
		}
	}
	
	@Http(url = "/wechat_auto_message_refresh", method = HttpMethod.POST)
	public void refresh(HttpRequest request)
	{
		wechatAutoMessageFacade.refresh();
	}
	
}
